package com.qf.driver.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.qf.common.bean.R;
import com.qf.common.dto.DeviceDto;
import com.qf.common.dto.DriverMqttDto;
import com.qf.common.dto.ProductDto;
import com.qf.common.model.QfDevice;
import com.qf.driver.feign.DeviceFeign;
import com.qf.driver.feign.ProductFeign;
import com.qf.driver.mqtt.bean.MqttProperties;
import com.qf.driver.service.AuthService;
import com.qf.driver.service.DeviceTopicService;
import com.qf.driver.service.MqttUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.ws.ServiceMode;
import java.util.List;

/**
 * @author 千锋健哥
 */
@Service
public class AuthServiceImpl implements AuthService {

    @Autowired
    private ProductFeign productFeign;

    @Autowired
    private DeviceFeign deviceFeign;

    @Autowired
    private MqttUserService mqttUserService;

    @Autowired
    private DeviceTopicService deviceTopicService;

    @Autowired
    private MqttProperties mqttProperties;

    @Override
    public R<DriverMqttDto> login(DriverMqttDto dto) {
        //1. 校验产品key是否正确
        ProductDto productDto = new ProductDto();
        productDto.setProductKey(dto.getProductKey());
        R<List> productResult = productFeign.findAreaAll(productDto);
        if (productResult == null) {
            return R.fail("产品不存在!");
        }
        if (productResult.getData() == null) {
            return R.fail("产品不存在!");
        }
        if (productResult.getData().size() == 0) {
            return R.fail("产品不存在!");
        }

        //2. 校验设备key是否正确
        DeviceDto deviceDto = new DeviceDto();
        deviceDto.setDeviceKey(dto.getDeviceKey());
        R<List> deviceResult = deviceFeign.findAreaAll(deviceDto);
        if (deviceResult == null) {
            return R.fail("设备不存在!");
        }
        if (deviceResult.getData() == null) {
            return R.fail("设备不存在!");
        }
        if (deviceResult.getData().size() == 0) {
            return R.fail("设备不存在!");
        }

        //3. 校验设备秘钥是否正确
        QfDevice device = BeanUtil.toBean(deviceResult.getData().get(0), QfDevice.class);
        if (!device.getDeviceSecret().equals(dto.getDeviceSecret())) {
            return R.fail("设备秘钥错误!");
        }

        //4. 更改设备激活状态, 由未激活改为已激活
        device.setActive(2);
        deviceFeign.update(device);

        //5. 到emqx账户表注册这个设备的emqx服务器账户, 使用租户id作为emqx服务器账号密码
        mqttUserService.addMqttUser(device.getTenantId());

        //6. 创建本平台和emqx服务器中的topic主题连接, 使用mqtt协议连接
        deviceTopicService.deviceTopicJoin(device.getDeviceKey());

        //7. 封装emqx服务器的账户, 密码, url地址, 端口号返回给硬件设备
        //  tcp://192.168.200.133:1883
        String url = mqttProperties.getUrl();
        // tcp:192.168.200.133:1883
        url = url.replaceAll("//", "");
        //tcp    192.168.200.133    1883
        String[] split = url.split(":");
        dto.setIp(split[1]);
        dto.setPort(Integer.parseInt(split[2]));
        dto.setUrl(url);
        dto.setUsername(device.getTenantId());
        dto.setPassword(device.getTenantId());
        dto.setTenantId(device.getTenantId());
        return R.ok(dto);
    }

}
